/**
 * @author Zou Yu
 * create at: 2022/3/21 16:36
 */

//给定一个n个元素有序的（升序）整型数组nums 和一个目标值target，写一个函数搜索nums中的target，如果目标值存在返回下标，否则返回 -1。

//输入: nums = [-1,0,3,5,9,12], target = 9
//输出: 4
//解释: 9 出现在 nums 中并且下标为 4

public class Solution20220321 {
    public static class Solution {
        public int search(int[] nums, int target) {
            int start = 0;
            int end = nums.length - 1;

            while (start <= end) {
                int temp = start + ((end - start) >> 1);
                if (nums[temp] > target) {
                    end = temp - 1;
                } else if (nums[temp] < target) {
                    start = temp + 1;
                }else {
                    return temp;
                }
            }
            return -1;
        }
    }

    public static void main(String[] args) {
        int [] nums = {5};
        int target = 5;
        int search = new Solution().search(nums, target);
        System.out.println(search);
    }
}